package DAO;


import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;

public class AddressDao {
  public AddressDao() {
  }

  public boolean creaAddress(int IdAddress, String street, String state,
                             String zipCode, String city, int clinicnumber,
                             int staffnumber, int ownernumber) {

    Conexion conexion = new Conexion();
    boolean creado = false;
    Connection conex;

    try {
      conex = conexion.getConnection();
      PreparedStatement ps = null;
      ResultSet rs = null;

      ResultSet rsa = null;
      int mayor = -1;
      ps = conex.prepareStatement("select max (ida)  from address");
      
      rsa = ps.executeQuery();
      if (rsa != null) {
        while (rsa.next()) {
          mayor = rsa.getInt(1);
        }
      } else
        IdAddress = 1;
      if(clinicnumber == -1)
      {
        if (staffnumber == -1)
        {
          ps =
              conex.prepareStatement("INSERT INTO ADDRESS(IDA, STREET,STATE,ZIPCODE,CITY,OWNERNUMBER) VALUES (?,?,?,?,?,?)");
          ps.setInt(6, ownernumber);    
        }
        else
        {
          ps =
              conex.prepareStatement("INSERT INTO ADDRESS(IDA, STREET,STATE,ZIPCODE,CITY,STAFFNUMBER) VALUES (?,?,?,?,?,?)");
          ps.setInt(6, staffnumber); 
        }
      }
      else
      {
        ps =
            conex.prepareStatement("INSERT INTO ADDRESS(IDA, STREET,STATE,ZIPCODE,CITY,CLINICNUMBER) VALUES (?,?,?,?,?,?)");
        ps.setInt(6, clinicnumber); 
      }
      IdAddress = mayor + 1;
      ps.setInt(1, IdAddress);
      ps.setString(2, street);
      ps.setString(3, state);
      ps.setString(4, zipCode);
      ps.setString(5, city);
      

      int numeroFilas = ps.executeUpdate();
      System.out.println("El numero de filas que inserto fueron" +
                         numeroFilas);
      creado = true;
      conex.close();

    } catch (SQLException ex) {
      System.err.println("SQLException: " + ex.getMessage());

    }
    return creado;
  }
  
  public boolean eliminarAddress(int IdAddress) {

    Conexion conexion = new Conexion();
    boolean eliminado = false;
    Connection conex;

    try {
      conex = conexion.getConnection();
      PreparedStatement ps = null;
      ResultSet rs = null;

      ps = conex.prepareStatement("DELETE FROM ADDRESS where IDA = ?");

      ps.setInt(1, IdAddress);


      int numeroFilas = ps.executeUpdate();
      System.out.println("El numero de filas que borro fueron" +
                         numeroFilas);
      eliminado = true;
      conex.close();

    } catch (SQLException ex) {
      System.err.println("SQLException: " + ex.getMessage());

    }
    return eliminado;
  }
  
    public boolean modificaAddressEstado(int IdAddress,  String state)
    {
      Conexion conexion = new Conexion();
      Connection conex;
      try {
        conex = conexion.getConnection();
        PreparedStatement ps = null;
        
        ps =
            conex.prepareStatement("UPDATE Address set State = ?  where ida = ?"); 
        
        ps.setString(1, state);
        
        ps.setInt(2, IdAddress);
        
        int numeroFilas = ps.executeUpdate();
        System.out.println("El numero de filas que actualizo fueron" +
                           numeroFilas);

        conex.close();
        
      } catch (SQLException ex) {
        System.err.println("SQLException: " + ex.getMessage());
        return false;
      }
      return true;
    }
    
    public boolean modificaAddressCalle(int IdAddress,  String calle)
    {
      Conexion conexion = new Conexion();
      Connection conex;
      try {
        conex = conexion.getConnection();
        PreparedStatement ps = null;
        
        ps =
            conex.prepareStatement("UPDATE Address set Street = ?  where ida = ?"); 
        
        ps.setString(1, calle);
        
        ps.setInt(2, IdAddress);
        
        int numeroFilas = ps.executeUpdate();
        System.out.println("El numero de filas que actualizo fueron" +
                           numeroFilas);

        conex.close();
        
      } catch (SQLException ex) {
        System.err.println("SQLException: " + ex.getMessage());
        return false;
      }
      return true;
    }
    
    public boolean modificaAddressZipCode(int IdAddress,  int zip)
    {
      Conexion conexion = new Conexion();
      Connection conex;
      try {
        conex = conexion.getConnection();
        PreparedStatement ps = null;
        
        ps =
            conex.prepareStatement("UPDATE Address set ZipCode = ?  where ida = ?"); 
        
        ps.setInt(1, zip);
        ps.setInt(2, IdAddress);
        
        int numeroFilas = ps.executeUpdate();
        System.out.println("El numero de filas que actualizo fueron" +
                           numeroFilas);

        conex.close();
        
      } catch (SQLException ex) {
        System.err.println("SQLException: " + ex.getMessage());
        return false;
      }
      return true;
    }
    
    public boolean modificaAddressCiudad(int IdAddress,  String ciudad)
    {
      Conexion conexion = new Conexion();
      Connection conex;
      try {
        conex = conexion.getConnection();
        PreparedStatement ps = null;
        
        ps =
            conex.prepareStatement("UPDATE Address set City = ?  where ida = ?"); 
        
        ps.setString(1, ciudad);
        
        ps.setInt(2, IdAddress);
        
        int numeroFilas = ps.executeUpdate();
        System.out.println("El numero de filas que actualizo fueron" +
                           numeroFilas);

        conex.close();
        
      } catch (SQLException ex) {
        System.err.println("SQLException: " + ex.getMessage());
        return false;
      }
      return true;
    }
    
    
    //Si pertenece a una Clinica a un Owner o Staff;
    public boolean modificaAddressPertenencia(int IdAddress, int cambio, String cual)
    {
      Conexion conexion = new Conexion();
      Connection conex;
      try {
        conex = conexion.getConnection();
        PreparedStatement ps = null;
        
        if (cual.equalsIgnoreCase("clinica"))
        ps = conex.prepareStatement("UPDATE Address set ClinicNumber=? ,StaffNumber=?, OwnerNumber=?  where ida = ?"); 
          else if (cual.equalsIgnoreCase("staff"))
              ps = conex.prepareStatement("UPDATE Address set StaffNumber=?, OwnerNumber=?,ClinicNumber=?  where ida = ?"); 
              else if (cual.equalsIgnoreCase("owner"))
                  ps = conex.prepareStatement("UPDATE Address set OwnerNumber=?,ClinicNumber=?,StaffNumber=? where ida = ?"); 
        else{
            System.out.println("Dato mal digitado,(AddressPertenencia.)");
            conex.close();
            return false;
        }
       
        ps.setInt(1, cambio);
          ps.setNull(2,Types.INTEGER);
          ps.setNull(3,Types.INTEGER);
        ps.setInt(4, IdAddress);
        
        int numeroFilas = ps.executeUpdate();
        System.out.println("El numero de filas que actualizo fueron" +
                           numeroFilas);

        conex.close();
        
      } catch (SQLException ex) {
        System.err.println("SQLException: " + ex.getMessage());
        return false;
      }
      return true;
    }
}
